/**
 * 
 */
package br.skytef.stmanager.daos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import br.skytef.stmanager.entities.UtcEntity;
import br.skytef.stmanager.utils.Pool;

/**
 * @author Ivam Henrique Marques dos Santos
 *         email: ivam.santos@skytef.com.br
 *         skype: ivam.santos.skytef
 *         telefone: +55 11 96214-7543
 */
public class UtcDAO {

	private static String erro;
	public static String getErro() {return erro;}

	private static Connection conexao = null;

	public boolean inserirUTC(UtcEntity utc ){
		try {
			conexao = Pool.getConnection();

			String query ="INSERT INTO utc(tarefa_id, cliente_idCliente, usuario_id, tempoAtend)"
					+ "VALUES(?,?,?,?);";
			PreparedStatement preparedStmt = conexao.prepareStatement(query);
			preparedStmt.setLong     		(1, utc.getTarefaId());
			preparedStmt.setInt     		(2, utc.getClienteId());
			preparedStmt.setInt     		(3, utc.getUsuarioId());
			preparedStmt.setTimestamp		(4, utc.getTempoAtend());
			preparedStmt.execute();			
			conexao.close();
			return true;

		} catch (SQLException e) {
			System.out.println("SQL erro: "+ e);
			erro=e.toString();
			return false;
		}catch (ClassCastException e) {
			System.out.println("Classe erro: "+ e);
			erro=e.toString();
			return false;
		} catch (Exception e) {
			System.out.println("Geral erro: "+ e);
			erro=e.toString();
			return false;
		}
	}

	public boolean atualizarUtc(UtcEntity utc) throws SQLException{
		try {
			conexao = Pool.getConnection();
			String query = "UPDATE utc SET tarefa_id=?,cliente_idCliente=?,usuario_id=? WHERE tarefa_id =? ;";
			PreparedStatement preparedStmt = conexao.prepareStatement(query);
			preparedStmt.setLong 		(1, utc.getTarefaId());
			preparedStmt.setInt 		(2, utc.getClienteId());
			preparedStmt.setInt 		(3, utc.getUsuarioId());
			preparedStmt.setLong		(4,utc.getTarefaId());
			preparedStmt.execute();	
			conexao.close();
			return true;

		} catch (SQLException e) {
			System.out.println("SQL erro: "+ e);
			conexao.close();
			return false;
		} catch (Exception e) {
			System.out.println("Geral erro: "+ e);
			conexao.close();
			return false;
		}
	}
}



